# Replication code for Taylor C. Boas and Amy Erica Smith, “Looks Like Me, Thinks Like Me: Descriptive Representation and Opinion Congruence in Brazil.” Latin American Research Review 54, 2 (2019).

# Analysis conducted in R 3.6.0 on MacOS 10.13.6

# NOTE: This file replicates main text Figure 1 and Appendix Figures 2 and 3. We recommend running R replication files in the following order; please see readme.txt for details.
# 	1_merge_lapop.R
# 	2_merge_latinobarometro.R
# 	3_recode_reshape.R
# 	4_difference_in_distributions.R
# 	5_regressions.R
# 	6_civil_society_meeting.R
# 	7_mass_descriptives.R
# 	8_elite_descriptives.R
# 	9_mean_differences.R
#	10_difference_in_distributions_ks.R
#	11_elite_sample_simulation.R
#	12_converts_vs_lifelong.R

# Set working directory as appropriate
# setwd('~/Dropbox/brazil_leg_surveys/replication/')

# Clean desktop and load packages. Please make sure all necessary packages are installed.

library(Hmisc)

rm(list=ls(all=T))

load('elite.data.RData')
load('mass.data.RData')

# ======================================================
# Main Text Figure 1: Opinion Congruence: Differences in
# Distributions 
# ======================================================

# Functions for calculating differences in CDFs

wtd.ecdf<-function(x,weights=NULL) function(y) {
	cdf<-wtd.Ecdf(x,weights=weights,normwt=T,type='i/n')
	cdf<-data.frame(x=cdf$x,Fx=cdf$ecdf)
	cdf$x[1]<- -Inf
	sapply(y,function(z) cdf$Fx[max(which(round(cdf$x,5) <= round(z,5)))]) # Dealing with differences in precision that were causing equal numbers to not be recognized as such
}

ecdf.diff<-function(x,y){
	vals<-sort(unique(c(x,y)))
	Fy<-ecdf(y)(vals)
	Fx<-ecdf(x)(vals)
	area<-0
	for(i in 1:(length(vals)-1)){
		area<-area+abs(diff(vals[i:(i+1)])*(Fx[i]-Fy[i]))
	}
	area
}

wtd.ecdf.diff<-function(x,y,weights.x,weights.y){
	vals<-sort(unique(c(x,y)))
	Fy<-wtd.ecdf(y,weights=weights.y)(vals)
	Fx<-wtd.ecdf(x,weights=weights.x)(vals)
	area<-0
	for(i in 1:(length(vals)-1)){
		area<-area+abs(diff(vals[i:(i+1)])*(Fx[i]-Fy[i]))
	}
	area
}

mass.data$mass_survey<-gsub('^([a-z]*)\\-{0,1}[0-9].*','\\1', mass.data$mass_id)

# Weights for economic regime preference, ideology. For masses, weighting each paired BLS wave equally. For elites, using proportions of each demographic category in each actual legislature.

mass.weight.econ.all<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)])))
mass.weight.econ.evang<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_evang)])))
mass.weight.econ.female<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_female)])))
mass.weight.econ.black<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_black)])))
mass.weight.econ.loweduc<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_loweduc)])))
mass.weight.econ.pt<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PT')])))
mass.weight.econ.psdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PSDB')])))
mass.weight.econ.pmdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PMDB')])))
mass.weight.econ.sp<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_uf=='SP')])))
mass.weight.econ.mg<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_uf=='MG')])))
mass.weight.ideol.all<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)])))
mass.weight.ideol.evang<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_evang)])))
mass.weight.ideol.female<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_female)])))
mass.weight.ideol.black<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_black)])))
mass.weight.ideol.loweduc<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_loweduc)])))
mass.weight.ideol.pt<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PT')])))
mass.weight.ideol.psdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PSDB')])))
mass.weight.ideol.pmdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PMDB')])))
mass.weight.ideol.sp<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_uf=='SP')])))
mass.weight.ideol.mg<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_uf=='MG')])))

# Separate mass weights for LB and AB-specific analysis of party congruence on ideology. For LB, restricting to the two most recent BLS waves which is where they overlap.

mass.weight.ideol.pt.lb<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PT')&(mass.data$mass_survey=='lb')&(mass.data$mass_blsyear!=2005)])))
mass.weight.ideol.pt.ab<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PT')&(mass.data$mass_survey %in% c('lapop','beps'))])))
mass.weight.ideol.pt.lbab<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PT')&(mass.data$mass_blsyear!=2005)])))
mass.weight.ideol.psdb.lb<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PSDB')&(mass.data$mass_survey=='lb')&(mass.data$mass_blsyear!=2005)])))
mass.weight.ideol.psdb.ab<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PSDB')&(mass.data$mass_survey %in% c('lapop','beps'))])))
mass.weight.ideol.psdb.lbab<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PSDB')&(mass.data$mass_blsyear!=2005)])))
mass.weight.ideol.pmdb.lb<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PMDB')&(mass.data$mass_survey=='lb')&(mass.data$mass_blsyear!=2005)])))
mass.weight.ideol.pmdb.ab<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PMDB')&(mass.data$mass_survey %in% c('lapop','beps'))])))
mass.weight.ideol.pmdb.lbab<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PMDB')&(mass.data$mass_blsyear!=2005)])))

mass.data$weight.econ.all<-ifelse(is.na(mass.data$mass_econ),NA,mass.weight.econ.all[match(mass.data$mass_blsyear, mass.weight.econ.all[,1]),2])
mass.data$weight.econ.evang<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_evang,NA,mass.weight.econ.evang[match(mass.data$mass_blsyear, mass.weight.econ.evang[,1]),2])
mass.data$weight.econ.female<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_female,NA,mass.weight.econ.female[match(mass.data$mass_blsyear, mass.weight.econ.female[,1]),2])
mass.data$weight.econ.black<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_black,NA,mass.weight.econ.black[match(mass.data$mass_blsyear, mass.weight.econ.black[,1]),2])
mass.data$weight.econ.loweduc<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_loweduc,NA,mass.weight.econ.loweduc[match(mass.data$mass_blsyear, mass.weight.econ.loweduc[,1]),2])
mass.data$weight.econ.pt<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PT',NA,mass.weight.econ.pt[match(mass.data$mass_blsyear, mass.weight.econ.pt[,1]),2])
mass.data$weight.econ.psdb<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PSDB',NA,mass.weight.econ.psdb[match(mass.data$mass_blsyear, mass.weight.econ.psdb[,1]),2])
mass.data$weight.econ.pmdb<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PMDB',NA,mass.weight.econ.pmdb[match(mass.data$mass_blsyear, mass.weight.econ.pmdb[,1]),2])
mass.data$weight.econ.sp<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_uf=='SP',NA,mass.weight.econ.sp[match(mass.data$mass_blsyear, mass.weight.econ.sp[,1]),2])
mass.data$weight.econ.mg<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_uf=='MG',NA,mass.weight.econ.mg[match(mass.data$mass_blsyear, mass.weight.econ.mg[,1]),2])
mass.data$weight.ideol.all<-ifelse(is.na(mass.data$mass_ideol),NA,mass.weight.ideol.all[match(mass.data$mass_blsyear, mass.weight.ideol.all[,1]),2])
mass.data$weight.ideol.evang<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_evang,NA,mass.weight.ideol.evang[match(mass.data$mass_blsyear, mass.weight.ideol.evang[,1]),2])
mass.data$weight.ideol.female<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_female,NA,mass.weight.ideol.female[match(mass.data$mass_blsyear, mass.weight.ideol.female[,1]),2])
mass.data$weight.ideol.black<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_black,NA,mass.weight.ideol.black[match(mass.data$mass_blsyear, mass.weight.ideol.black[,1]),2])
mass.data$weight.ideol.loweduc<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_loweduc,NA,mass.weight.ideol.loweduc[match(mass.data$mass_blsyear, mass.weight.ideol.loweduc[,1]),2])
mass.data$weight.ideol.pt<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PT',NA,mass.weight.ideol.pt[match(mass.data$mass_blsyear, mass.weight.ideol.pt[,1]),2])
mass.data$weight.ideol.psdb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PSDB',NA,mass.weight.ideol.psdb[match(mass.data$mass_blsyear, mass.weight.ideol.psdb[,1]),2])
mass.data$weight.ideol.pmdb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PMDB',NA,mass.weight.ideol.pmdb[match(mass.data$mass_blsyear, mass.weight.ideol.pmdb[,1]),2])
mass.data$weight.ideol.sp<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_uf=='SP',NA,mass.weight.ideol.sp[match(mass.data$mass_blsyear, mass.weight.ideol.sp[,1]),2])
mass.data$weight.ideol.mg<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_uf=='MG',NA,mass.weight.ideol.mg[match(mass.data$mass_blsyear, mass.weight.ideol.mg[,1]),2])

mass.data$weight.ideol.pt.lb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PT'|mass.data$mass_blsyear==2005|!mass.data$mass_survey=='lb',NA,mass.weight.ideol.pt.lb[match(mass.data$mass_blsyear, mass.weight.ideol.pt.lb[,1]),2])
mass.data$weight.ideol.pt.ab<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PT'|mass.data$mass_blsyear==2005|!mass.data$mass_survey %in% c('lapop','beps'),NA,mass.weight.ideol.pt.ab[match(mass.data$mass_blsyear, mass.weight.ideol.pt.ab[,1]),2])
mass.data$weight.ideol.pt.lbab<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PT'|mass.data$mass_blsyear==2005,NA,mass.weight.ideol.pt.lbab[match(mass.data$mass_blsyear, mass.weight.ideol.pt.lbab[,1]),2])
mass.data$weight.ideol.psdb.lb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PSDB'|mass.data$mass_blsyear==2005|!mass.data$mass_survey=='lb',NA,mass.weight.ideol.psdb.lb[match(mass.data$mass_blsyear, mass.weight.ideol.psdb.lb[,1]),2])
mass.data$weight.ideol.psdb.ab<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PSDB'|mass.data$mass_blsyear==2005|!mass.data$mass_survey %in% c('lapop','beps'),NA,mass.weight.ideol.psdb.ab[match(mass.data$mass_blsyear, mass.weight.ideol.psdb.ab[,1]),2])
mass.data$weight.ideol.psdb.lbab<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PSDB'|mass.data$mass_blsyear==2005,NA,mass.weight.ideol.psdb.lbab[match(mass.data$mass_blsyear, mass.weight.ideol.psdb.lbab[,1]),2])
mass.data$weight.ideol.pmdb.lb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PMDB'|mass.data$mass_blsyear==2005|!mass.data$mass_survey=='lb',NA,mass.weight.ideol.pmdb.lb[match(mass.data$mass_blsyear, mass.weight.ideol.pmdb.lb[,1]),2])
mass.data$weight.ideol.pmdb.ab<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PMDB'|mass.data$mass_blsyear==2005|!mass.data$mass_survey %in% c('lapop','beps'),NA,mass.weight.ideol.pmdb.ab[match(mass.data$mass_blsyear, mass.weight.ideol.pmdb.ab[,1]),2])
mass.data$weight.ideol.pmdb.lbab<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PMDB'|mass.data$mass_blsyear==2005,NA,mass.weight.ideol.pmdb.lbab[match(mass.data$mass_blsyear, mass.weight.ideol.pmdb.lbab[,1]),2])

elite.weight.econ.all<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)])))
elite.weight.econ.evang<-data.frame(c(.4,.2,.4)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_evang)])))
elite.weight.econ.female<-data.frame(c(.31,.345,.345)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_female)])))
elite.weight.econ.black<-data.frame((1/2)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_black)])))
elite.weight.econ.loweduc<-data.frame(c(.375,.3,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_loweduc)])))
elite.weight.econ.pt<-data.frame(c(.34,.32,.34)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PT')])))
elite.weight.econ.psdb<-data.frame(c(.37,.34,.29)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PSDB')])))
elite.weight.econ.pmdb<-data.frame(c(.325,.35,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PMDB')])))
elite.weight.econ.sp<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_uf=='SP')])))
elite.weight.econ.mg<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_uf=='MG')])))
elite.weight.ideol.all<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)])))
elite.weight.ideol.evang<-data.frame(c(.4,.2,.4)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_evang)])))
elite.weight.ideol.female<-data.frame(c(.31,.345,.345)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_female)])))
elite.weight.ideol.black<-data.frame((1/2)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_black)])))
elite.weight.ideol.loweduc<-data.frame(c(.375,.3,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_loweduc)])))
elite.weight.ideol.pt<-data.frame(c(.34,.32,.34)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PT')])))
elite.weight.ideol.psdb<-data.frame(c(.37,.34,.29)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PSDB')])))
elite.weight.ideol.pmdb<-data.frame(c(.325,.35,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PMDB')])))
elite.weight.ideol.sp<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_uf=='SP')])))
elite.weight.ideol.mg<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_uf=='MG')])))

# Separate elite weights for LB and AB-specific analysis of party congruence on ideology. Restricting to the two most recent BLS waves which is where they overlap.

elite.weight.ideol.pt.lbab<-data.frame(c(.48,.52)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PT')&(elite.data$elite_year!=2005)])))
elite.weight.ideol.psdb.lbab<-data.frame(c(.54,.46)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PSDB')&(elite.data$elite_year!=2005)])))
elite.weight.ideol.pmdb.lbab<-data.frame(c(.52,.48)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PMDB')&(elite.data$elite_year!=2005)])))

elite.data$weight.econ.all<-ifelse(is.na(elite.data$elite_econ),NA,elite.weight.econ.all[match(elite.data$elite_year, elite.weight.econ.all[,1]),2])
elite.data$weight.econ.evang<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_evang,NA,elite.weight.econ.evang[match(elite.data$elite_year, elite.weight.econ.evang[,1]),2])
elite.data$weight.econ.female<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_female,NA,elite.weight.econ.female[match(elite.data$elite_year, elite.weight.econ.female[,1]),2])
elite.data$weight.econ.black<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_black,NA,elite.weight.econ.black[match(elite.data$elite_year, elite.weight.econ.black[,1]),2])
elite.data$weight.econ.loweduc<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_loweduc,NA,elite.weight.econ.loweduc[match(elite.data$elite_year, elite.weight.econ.loweduc[,1]),2])
elite.data$weight.econ.pt<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PT',NA,elite.weight.econ.pt[match(elite.data$elite_year, elite.weight.econ.pt[,1]),2])
elite.data$weight.econ.psdb<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PSDB',NA,elite.weight.econ.psdb[match(elite.data$elite_year, elite.weight.econ.psdb[,1]),2])
elite.data$weight.econ.pmdb<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PMDB',NA,elite.weight.econ.pmdb[match(elite.data$elite_year, elite.weight.econ.pmdb[,1]),2])
elite.data$weight.econ.sp<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_uf=='SP',NA,elite.weight.econ.sp[match(elite.data$elite_year, elite.weight.econ.sp[,1]),2])
elite.data$weight.econ.mg<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_uf=='MG',NA,elite.weight.econ.mg[match(elite.data$elite_year, elite.weight.econ.mg[,1]),2])
elite.data$weight.ideol.all<-ifelse(is.na(elite.data$elite_ideol),NA,elite.weight.ideol.all[match(elite.data$elite_year, elite.weight.ideol.all[,1]),2])
elite.data$weight.ideol.evang<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_evang,NA,elite.weight.ideol.evang[match(elite.data$elite_year, elite.weight.ideol.evang[,1]),2])
elite.data$weight.ideol.female<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_female,NA,elite.weight.ideol.female[match(elite.data$elite_year, elite.weight.ideol.female[,1]),2])
elite.data$weight.ideol.black<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_black,NA,elite.weight.ideol.black[match(elite.data$elite_year, elite.weight.ideol.black[,1]),2])
elite.data$weight.ideol.loweduc<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_loweduc,NA,elite.weight.ideol.loweduc[match(elite.data$elite_year, elite.weight.ideol.loweduc[,1]),2])
elite.data$weight.ideol.pt<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PT',NA,elite.weight.ideol.pt[match(elite.data$elite_year, elite.weight.ideol.pt[,1]),2])
elite.data$weight.ideol.psdb<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PSDB',NA,elite.weight.ideol.psdb[match(elite.data$elite_year, elite.weight.ideol.psdb[,1]),2])
elite.data$weight.ideol.pmdb<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PMDB',NA,elite.weight.ideol.pmdb[match(elite.data$elite_year, elite.weight.ideol.pmdb[,1]),2])
elite.data$weight.ideol.sp<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_uf=='SP',NA,elite.weight.ideol.sp[match(elite.data$elite_year, elite.weight.ideol.sp[,1]),2])
elite.data$weight.ideol.mg<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_uf=='MG',NA,elite.weight.ideol.mg[match(elite.data$elite_year, elite.weight.ideol.mg[,1]),2])

elite.data$weight.ideol.pt.lbab<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PT'| elite.data$elite_year==2005,NA,elite.weight.ideol.pt.lbab[match(elite.data$elite_year, elite.weight.ideol.pt.lbab[,1]),2])
elite.data$weight.ideol.psdb.lbab<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PSDB'| elite.data$elite_year==2005,NA,elite.weight.ideol.psdb.lbab[match(elite.data$elite_year, elite.weight.ideol.psdb.lbab[,1]),2])
elite.data$weight.ideol.pmdb.lbab<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PMDB'| elite.data$elite_year==2005,NA,elite.weight.ideol.pmdb.lbab[match(elite.data$elite_year, elite.weight.ideol.pmdb.lbab[,1]),2])


# Differences

diff.econ.all<-wtd.ecdf.diff(elite.data$elite_econ,mass.data$mass_econ,elite.data$weight.econ.all,mass.data$mass_pweight*mass.data$weight.econ.all)
diff.econ.evang<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_evang],mass.data$mass_econ[mass.data$mass_evang],elite.data$weight.econ.evang[elite.data$elite_evang],mass.data$mass_pweight[mass.data$mass_evang]*mass.data$weight.econ.evang[mass.data$mass_evang])
diff.econ.female<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_female],mass.data$mass_econ[mass.data$mass_female],elite.data$weight.econ.female[elite.data$elite_female],mass.data$mass_pweight[mass.data$mass_female]*mass.data$weight.econ.female[mass.data$mass_female])
diff.econ.black<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_black],mass.data$mass_econ[mass.data$mass_black],elite.data$weight.econ.black[elite.data$elite_black],mass.data$mass_pweight[mass.data$mass_black]*mass.data$weight.econ.black[mass.data$mass_black])
diff.econ.loweduc<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_loweduc],mass.data$mass_econ[mass.data$mass_loweduc],elite.data$weight.econ.loweduc[elite.data$elite_loweduc],mass.data$mass_pweight[mass.data$mass_loweduc]*mass.data$weight.econ.loweduc[mass.data$mass_loweduc])
diff.econ.pt<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_partythen=='PT'],mass.data$mass_econ[mass.data$mass_party=='PT'],elite.data$weight.econ.pt[elite.data$elite_partythen=='PT'],mass.data$mass_pweight[mass.data$mass_party=='PT']*mass.data$weight.econ.pt[mass.data$mass_party=='PT'])
diff.econ.psdb<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_partythen=='PSDB'],mass.data$mass_econ[mass.data$mass_party=='PSDB'],elite.data$weight.econ.psdb[elite.data$elite_partythen=='PSDB'],mass.data$mass_pweight[mass.data$mass_party=='PSDB']*mass.data$weight.econ.psdb[mass.data$mass_party=='PSDB'])
diff.econ.pmdb<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_partythen=='PMDB'],mass.data$mass_econ[mass.data$mass_party=='PMDB'],elite.data$weight.econ.pmdb[elite.data$elite_partythen=='PMDB'],mass.data$mass_pweight[mass.data$mass_party=='PMDB']*mass.data$weight.econ.pmdb[mass.data$mass_party=='PMDB'])
diff.econ.sp<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_uf=='SP'],mass.data$mass_econ[mass.data$mass_uf=='SP'],elite.data$weight.econ.sp[elite.data$elite_uf=='SP'],mass.data$mass_pweight[mass.data$mass_uf=='SP']*mass.data$weight.econ.sp[mass.data$mass_uf=='SP'])
diff.econ.mg<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_uf=='MG'],mass.data$mass_econ[mass.data$mass_uf=='MG'],elite.data$weight.econ.mg[elite.data$elite_uf=='MG'],mass.data$mass_pweight[mass.data$mass_uf=='MG']*mass.data$weight.econ.mg[mass.data$mass_uf=='MG'])

# Econ differences, disaggregated by BLS wave

diff.econ.all.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005],mass.data$mass_econ[mass.data$mass_blsyear==2005],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005])
diff.econ.evang.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005&elite.data$elite_evang],mass.data$mass_econ[mass.data$mass_blsyear==2005&mass.data$mass_evang],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_evang])
diff.econ.female.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005&elite.data$elite_female],mass.data$mass_econ[mass.data$mass_blsyear==2005&mass.data$mass_female],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_female])
diff.econ.loweduc.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005&elite.data$elite_loweduc],mass.data$mass_econ[mass.data$mass_blsyear==2005&mass.data$mass_loweduc],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_loweduc])
diff.econ.pt.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005& elite.data$elite_partythen=='PT'],mass.data$mass_econ[mass.data$mass_blsyear==2005& mass.data$mass_party=='PT'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_party=='PT'])
diff.econ.psdb.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005& elite.data$elite_partythen=='PSDB'],mass.data$mass_econ[mass.data$mass_blsyear==2005&mass.data$mass_party=='PSDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_party=='PSDB'])
diff.econ.pmdb.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005& elite.data$elite_partythen=='PMDB'],mass.data$mass_econ[mass.data$mass_blsyear==2005&mass.data$mass_party=='PMDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_party=='PMDB'])
diff.econ.sp.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005&elite.data$elite_uf=='SP'],mass.data$mass_econ[mass.data$mass_blsyear==2005&mass.data$mass_uf=='SP'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_uf=='SP'])
diff.econ.mg.0205<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2005&elite.data$elite_uf=='MG'],mass.data$mass_econ[mass.data$mass_blsyear==2005&mass.data$mass_uf=='MG'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_uf=='MG'])

diff.econ.all.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009],mass.data$mass_econ[mass.data$mass_blsyear==2009],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009])
diff.econ.evang.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009&elite.data$elite_evang],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_evang],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_evang])
diff.econ.female.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009&elite.data$elite_female],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_female],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_female])
diff.econ.black.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009&elite.data$elite_black],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_black],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_black])
diff.econ.loweduc.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009&elite.data$elite_loweduc],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_loweduc],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_loweduc])
diff.econ.pt.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009& elite.data$elite_partythen=='PT'],mass.data$mass_econ[mass.data$mass_blsyear==2009& mass.data$mass_party=='PT'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_party=='PT'])
diff.econ.psdb.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009& elite.data$elite_partythen=='PSDB'],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_party=='PSDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_party=='PSDB'])
diff.econ.pmdb.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009& elite.data$elite_partythen=='PMDB'],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_party=='PMDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_party=='PMDB'])
diff.econ.sp.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009&elite.data$elite_uf=='SP'],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_uf=='SP'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_uf=='SP'])
diff.econ.mg.0609<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2009&elite.data$elite_uf=='MG'],mass.data$mass_econ[mass.data$mass_blsyear==2009&mass.data$mass_uf=='MG'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_uf=='MG'])

diff.econ.all.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013],mass.data$mass_econ[mass.data$mass_blsyear==2013],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013])
diff.econ.evang.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013&elite.data$elite_evang],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_evang],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_evang])
diff.econ.female.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013&elite.data$elite_female],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_female],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_female])
diff.econ.black.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013&elite.data$elite_black],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_black],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_black])
diff.econ.loweduc.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013&elite.data$elite_loweduc],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_loweduc],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_loweduc])
diff.econ.pt.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013& elite.data$elite_partythen=='PT'],mass.data$mass_econ[mass.data$mass_blsyear==2013& mass.data$mass_party=='PT'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_party=='PT'])
diff.econ.psdb.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013& elite.data$elite_partythen=='PSDB'],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_party=='PSDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_party=='PSDB'])
diff.econ.pmdb.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013& elite.data$elite_partythen=='PMDB'],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_party=='PMDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_party=='PMDB'])
diff.econ.sp.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013&elite.data$elite_uf=='SP'],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_uf=='SP'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_uf=='SP'])
diff.econ.mg.1013<-wtd.ecdf.diff(elite.data$elite_econ[elite.data$elite_year==2013&elite.data$elite_uf=='MG'],mass.data$mass_econ[mass.data$mass_blsyear==2013&mass.data$mass_uf=='MG'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_uf=='MG'])

diff.ideol.all<-wtd.ecdf.diff(elite.data$elite_ideol,mass.data$mass_ideol,elite.data$weight.ideol.all,mass.data$mass_pweight*mass.data$weight.ideol.all)
diff.ideol.evang<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_evang],mass.data$mass_ideol[mass.data$mass_evang],elite.data$weight.ideol.evang[elite.data$elite_evang],mass.data$mass_pweight[mass.data$mass_evang]*mass.data$weight.ideol.evang[mass.data$mass_evang])
diff.ideol.female<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_female],mass.data$mass_ideol[mass.data$mass_female],elite.data$weight.ideol.female[elite.data$elite_female],mass.data$mass_pweight[mass.data$mass_female]*mass.data$weight.ideol.female[mass.data$mass_female])
diff.ideol.black<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_black],mass.data$mass_ideol[mass.data$mass_black],elite.data$weight.ideol.black[elite.data$elite_black],mass.data$mass_pweight[mass.data$mass_black]*mass.data$weight.ideol.black[mass.data$mass_black])
diff.ideol.loweduc<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_loweduc],mass.data$mass_ideol[mass.data$mass_loweduc],elite.data$weight.ideol.loweduc[elite.data$elite_loweduc],mass.data$mass_pweight[mass.data$mass_loweduc]*mass.data$weight.ideol.loweduc[mass.data$mass_loweduc])
diff.ideol.pt<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PT'],mass.data$mass_ideol[mass.data$mass_party=='PT'],elite.data$weight.ideol.pt[elite.data$elite_partythen=='PT'],mass.data$mass_pweight[mass.data$mass_party=='PT']*mass.data$weight.ideol.pt[mass.data$mass_party=='PT'])
diff.ideol.psdb<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PSDB'],mass.data$mass_ideol[mass.data$mass_party=='PSDB'],elite.data$weight.ideol.psdb[elite.data$elite_partythen=='PSDB'],mass.data$mass_pweight[mass.data$mass_party=='PSDB']*mass.data$weight.ideol.psdb[mass.data$mass_party=='PSDB'])
diff.ideol.pmdb<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PMDB'],mass.data$mass_ideol[mass.data$mass_party=='PMDB'],elite.data$weight.ideol.pmdb[elite.data$elite_partythen=='PMDB'],mass.data$mass_pweight[mass.data$mass_party=='PMDB']*mass.data$weight.ideol.pmdb[mass.data$mass_party=='PMDB'])
diff.ideol.sp<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_uf=='SP'],mass.data$mass_ideol[mass.data$mass_uf=='SP'],elite.data$weight.ideol.sp[elite.data$elite_uf=='SP'],mass.data$mass_pweight[mass.data$mass_uf=='SP']*mass.data$weight.ideol.sp[mass.data$mass_uf=='SP'])
diff.ideol.mg<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_uf=='MG'],mass.data$mass_ideol[mass.data$mass_uf=='MG'],elite.data$weight.ideol.mg[elite.data$elite_uf=='MG'],mass.data$mass_pweight[mass.data$mass_uf=='MG']*mass.data$weight.ideol.mg[mass.data$mass_uf=='MG'])

# For party, separate calculations with different weights for LB and for AB/BEPS.

diff.ideol.pt.lb<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PT'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb'],elite.data$weight.ideol.pt.lbab[elite.data$elite_partythen=='PT'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb']*mass.data$weight.ideol.pt.lb[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb'])
diff.ideol.pt.ab<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PT'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')],elite.data$weight.ideol.pt.lbab[elite.data$elite_partythen=='PT'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')]*mass.data$weight.ideol.pt.ab[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')])
diff.ideol.pt.lbab<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PT'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005],elite.data$weight.ideol.pt.lbab[elite.data$elite_partythen=='PT'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005]*mass.data$weight.ideol.pt.lbab[mass.data$mass_party=='PT'&mass.data$mass_blsyear!=2005])

diff.ideol.psdb.lb<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PSDB'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb'],elite.data$weight.ideol.psdb.lbab[elite.data$elite_partythen=='PSDB'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb']*mass.data$weight.ideol.psdb.lb[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb'])
diff.ideol.psdb.ab<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PSDB'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')],elite.data$weight.ideol.psdb.lbab[elite.data$elite_partythen=='PSDB'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')]*mass.data$weight.ideol.psdb.ab[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')])
diff.ideol.psdb.lbab<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PSDB'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005],elite.data$weight.ideol.psdb.lbab[elite.data$elite_partythen=='PSDB'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005]*mass.data$weight.ideol.psdb.lbab[mass.data$mass_party=='PSDB'&mass.data$mass_blsyear!=2005])

diff.ideol.pmdb.lb<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PMDB'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb'],elite.data$weight.ideol.pmdb.lbab[elite.data$elite_partythen=='PMDB'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb']*mass.data$weight.ideol.pmdb.lb[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey=='lb'])
diff.ideol.pmdb.ab<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PMDB'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')],elite.data$weight.ideol.pmdb.lbab[elite.data$elite_partythen=='PMDB'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')]*mass.data$weight.ideol.pmdb.ab[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005&mass.data$mass_survey %in% c('lapop','beps')])
diff.ideol.pmdb.lbab<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_partythen=='PMDB'&elite.data$elite_year!=2005],mass.data$mass_ideol[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005],elite.data$weight.ideol.pmdb.lbab[elite.data$elite_partythen=='PMDB'&elite.data$elite_year!=2005],mass.data$mass_pweight[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005]*mass.data$weight.ideol.pmdb.lbab[mass.data$mass_party=='PMDB'&mass.data$mass_blsyear!=2005])

# Ideology differences, disaggregated by BLS wave

diff.ideol.all.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005],mass.data$mass_ideol[mass.data$mass_blsyear==2005],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005])
diff.ideol.evang.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005&elite.data$elite_evang],mass.data$mass_ideol[mass.data$mass_blsyear==2005&mass.data$mass_evang],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_evang])
diff.ideol.female.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005&elite.data$elite_female],mass.data$mass_ideol[mass.data$mass_blsyear==2005&mass.data$mass_female],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_female])
diff.ideol.loweduc.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005&elite.data$elite_loweduc],mass.data$mass_ideol[mass.data$mass_blsyear==2005&mass.data$mass_loweduc],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_loweduc])
diff.ideol.pt.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005& elite.data$elite_partythen=='PT'],mass.data$mass_ideol[mass.data$mass_blsyear==2005& mass.data$mass_party=='PT'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_party=='PT'])
diff.ideol.psdb.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005& elite.data$elite_partythen=='PSDB'],mass.data$mass_ideol[mass.data$mass_blsyear==2005&mass.data$mass_party=='PSDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_party=='PSDB'])
diff.ideol.pmdb.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005& elite.data$elite_partythen=='PMDB'],mass.data$mass_ideol[mass.data$mass_blsyear==2005&mass.data$mass_party=='PMDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_party=='PMDB'])
diff.ideol.sp.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005&elite.data$elite_uf=='SP'],mass.data$mass_ideol[mass.data$mass_blsyear==2005&mass.data$mass_uf=='SP'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_uf=='SP'])
diff.ideol.mg.0205<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2005&elite.data$elite_uf=='MG'],mass.data$mass_ideol[mass.data$mass_blsyear==2005&mass.data$mass_uf=='MG'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2005&mass.data$mass_uf=='MG'])

diff.ideol.all.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009],mass.data$mass_ideol[mass.data$mass_blsyear==2009],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009])
diff.ideol.evang.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009&elite.data$elite_evang],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_evang],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_evang])
diff.ideol.female.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009&elite.data$elite_female],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_female],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_female])
diff.ideol.black.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009&elite.data$elite_black],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_black],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_black])
diff.ideol.loweduc.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009&elite.data$elite_loweduc],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_loweduc],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_loweduc])
diff.ideol.pt.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009& elite.data$elite_partythen=='PT'],mass.data$mass_ideol[mass.data$mass_blsyear==2009& mass.data$mass_party=='PT'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_party=='PT'])
diff.ideol.psdb.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009& elite.data$elite_partythen=='PSDB'],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_party=='PSDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_party=='PSDB'])
diff.ideol.pmdb.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009& elite.data$elite_partythen=='PMDB'],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_party=='PMDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_party=='PMDB'])
diff.ideol.sp.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009&elite.data$elite_uf=='SP'],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_uf=='SP'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_uf=='SP'])
diff.ideol.mg.0609<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2009&elite.data$elite_uf=='MG'],mass.data$mass_ideol[mass.data$mass_blsyear==2009&mass.data$mass_uf=='MG'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2009&mass.data$mass_uf=='MG'])

diff.ideol.all.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013],mass.data$mass_ideol[mass.data$mass_blsyear==2013],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013])
diff.ideol.evang.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013&elite.data$elite_evang],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_evang],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_evang])
diff.ideol.female.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013&elite.data$elite_female],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_female],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_female])
diff.ideol.black.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013&elite.data$elite_black],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_black],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_black])
diff.ideol.loweduc.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013&elite.data$elite_loweduc],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_loweduc],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_loweduc])
diff.ideol.pt.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013& elite.data$elite_partythen=='PT'],mass.data$mass_ideol[mass.data$mass_blsyear==2013& mass.data$mass_party=='PT'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_party=='PT'])
diff.ideol.psdb.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013& elite.data$elite_partythen=='PSDB'],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_party=='PSDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_party=='PSDB'])
diff.ideol.pmdb.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013& elite.data$elite_partythen=='PMDB'],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_party=='PMDB'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_party=='PMDB'])
diff.ideol.sp.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013&elite.data$elite_uf=='SP'],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_uf=='SP'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_uf=='SP'])
diff.ideol.mg.1013<-wtd.ecdf.diff(elite.data$elite_ideol[elite.data$elite_year==2013&elite.data$elite_uf=='MG'],mass.data$mass_ideol[mass.data$mass_blsyear==2013&mass.data$mass_uf=='MG'],NULL,mass.data$mass_pweight[mass.data$mass_blsyear==2013&mass.data$mass_uf=='MG'])

diff.affirm.all<-wtd.ecdf.diff(elite.data$elite_affirm,mass.data$mass_affirm, NULL,mass.data$mass_pweight)
diff.affirm.evang<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_evang],mass.data$mass_affirm[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
diff.affirm.female<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_female],mass.data$mass_affirm[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
diff.affirm.black<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_black],mass.data$mass_affirm[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
diff.affirm.loweduc<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_loweduc],mass.data$mass_affirm[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
diff.affirm.pt<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_partythen=='PT'],mass.data$mass_affirm[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
diff.affirm.psdb<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_partythen=='PSDB'],mass.data$mass_affirm[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
diff.affirm.pmdb<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_partythen=='PMDB'],mass.data$mass_affirm[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
diff.affirm.sp<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_uf=='SP'],mass.data$mass_affirm[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
diff.affirm.mg<-wtd.ecdf.diff(elite.data$elite_affirm[elite.data$elite_uf=='MG'],mass.data$mass_affirm[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

diff.classaffirm.all<-wtd.ecdf.diff(elite.data$elite_classaffirm,mass.data$mass_classaffirm, NULL,mass.data$mass_pweight)
diff.classaffirm.evang<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_evang],mass.data$mass_classaffirm[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
diff.classaffirm.female<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_female],mass.data$mass_classaffirm[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
diff.classaffirm.black<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_black],mass.data$mass_classaffirm[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
diff.classaffirm.loweduc<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_loweduc],mass.data$mass_classaffirm[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
diff.classaffirm.pt<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_partythen=='PT'],mass.data$mass_classaffirm[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
diff.classaffirm.psdb<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_partythen=='PSDB'],mass.data$mass_classaffirm[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
diff.classaffirm.pmdb<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_partythen=='PMDB'],mass.data$mass_classaffirm[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
diff.classaffirm.sp<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_uf=='SP'],mass.data$mass_classaffirm[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
diff.classaffirm.mg<-wtd.ecdf.diff(elite.data$elite_classaffirm[elite.data$elite_uf=='MG'],mass.data$mass_classaffirm[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

diff.abortion.all<-wtd.ecdf.diff(elite.data$elite_abortion,mass.data$mass_abortion, NULL,mass.data$mass_pweight)
diff.abortion.evang<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_evang],mass.data$mass_abortion[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
diff.abortion.female<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_female],mass.data$mass_abortion[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
diff.abortion.black<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_black],mass.data$mass_abortion[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
diff.abortion.loweduc<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_loweduc],mass.data$mass_abortion[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
diff.abortion.pt<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_partythen=='PT'],mass.data$mass_abortion[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
diff.abortion.psdb<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_partythen=='PSDB'],mass.data$mass_abortion[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
diff.abortion.pmdb<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_partythen=='PMDB'],mass.data$mass_abortion[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
diff.abortion.sp<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_uf=='SP'],mass.data$mass_abortion[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
diff.abortion.mg<-wtd.ecdf.diff(elite.data$elite_abortion[elite.data$elite_uf=='MG'],mass.data$mass_abortion[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])


diff.gaymarriage.all<-wtd.ecdf.diff(elite.data$elite_gaymarriage,mass.data$mass_gaymarriage, NULL,mass.data$mass_pweight)
diff.gaymarriage.evang<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_evang],mass.data$mass_gaymarriage[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
diff.gaymarriage.female<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_female],mass.data$mass_gaymarriage[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
diff.gaymarriage.black<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_black],mass.data$mass_gaymarriage[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
diff.gaymarriage.loweduc<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_loweduc],mass.data$mass_gaymarriage[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
diff.gaymarriage.pt<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PT'],mass.data$mass_gaymarriage[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
diff.gaymarriage.psdb<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PSDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
diff.gaymarriage.pmdb<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PMDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
diff.gaymarriage.sp<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_uf=='SP'],mass.data$mass_gaymarriage[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
diff.gaymarriage.mg<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_uf=='MG'],mass.data$mass_gaymarriage[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

# For party, separate calculations for LB and for AB/BEPS (the latter weighted by mass_pweight)
diff.gaymarriage.pt.lb<-ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PT'],mass.data$mass_gaymarriage[mass.data$mass_party=='PT'&mass.data$mass_survey=='lb'])
diff.gaymarriage.pt.ab<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PT'],mass.data$mass_gaymarriage[mass.data$mass_party=='PT'&mass.data$mass_survey %in% c('lapop','beps')], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'&mass.data$mass_survey %in% c('lapop','beps')])
diff.gaymarriage.psdb.lb<-ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PSDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PSDB'&mass.data$mass_survey=='lb'])
diff.gaymarriage.psdb.ab<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PSDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PSDB'&mass.data$mass_survey %in% c('lapop','beps')], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'&mass.data$mass_survey %in% c('lapop','beps')])
diff.gaymarriage.pmdb.lb<-ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PMDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PMDB'&mass.data$mass_survey=='lb'])
diff.gaymarriage.pmdb.ab<-wtd.ecdf.diff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PMDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PMDB'&mass.data$mass_survey %in% c('lapop','beps')], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'&mass.data$mass_survey %in% c('lapop','beps')])

# Environment: unweighted because not drawing from BEPS or AB 2012.

diff.environment.all<-ecdf.diff(elite.data$elite_environment,mass.data$mass_environment)
diff.environment.evang<-ecdf.diff(elite.data$elite_environment[elite.data$elite_evang],mass.data$mass_environment[mass.data$mass_evang])
diff.environment.female<-ecdf.diff(elite.data$elite_environment[elite.data$elite_female],mass.data$mass_environment[mass.data$mass_female])
diff.environment.black<-ecdf.diff(elite.data$elite_environment[elite.data$elite_black],mass.data$mass_environment[mass.data$mass_black])
diff.environment.loweduc<-ecdf.diff(elite.data$elite_environment[elite.data$elite_loweduc],mass.data$mass_environment[mass.data$mass_loweduc])
diff.environment.pt<-ecdf.diff(elite.data$elite_environment[elite.data$elite_partythen=='PT'],mass.data$mass_environment[mass.data$mass_party=='PT'])
diff.environment.psdb<-ecdf.diff(elite.data$elite_environment[elite.data$elite_partythen=='PSDB'],mass.data$mass_environment[mass.data$mass_party=='PSDB'])
diff.environment.pmdb<-ecdf.diff(elite.data$elite_environment[elite.data$elite_partythen=='PMDB'],mass.data$mass_environment[mass.data$mass_party=='PMDB'])
diff.environment.sp<-ecdf.diff(elite.data$elite_environment[elite.data$elite_uf=='SP'],mass.data$mass_environment[mass.data$mass_uf=='SP'])
diff.environment.mg<-ecdf.diff(elite.data$elite_environment[elite.data$elite_uf=='MG'],mass.data$mass_environment[mass.data$mass_uf=='MG'])

ecdf.table<-matrix(c(
	diff.econ.all, diff.econ.evang, diff.econ.female, diff.econ.black, diff.econ.loweduc, diff.econ.pt, diff.econ.psdb, diff.econ.pmdb, diff.econ.sp, diff.econ.mg,
	diff.ideol.all, diff.ideol.evang, diff.ideol.female, diff.ideol.black, diff.ideol.loweduc, diff.ideol.pt, diff.ideol.psdb, diff.ideol.pmdb, diff.ideol.sp, diff.ideol.mg,
	diff.affirm.all, diff.affirm.evang, diff.affirm.female, diff.affirm.black, diff.affirm.loweduc, diff.affirm.pt, diff.affirm.psdb, diff.affirm.pmdb, diff.affirm.sp, diff.affirm.mg,
	diff.classaffirm.all, diff.classaffirm.evang, diff.classaffirm.female, diff.classaffirm.black, diff.classaffirm.loweduc, diff.classaffirm.pt, diff.classaffirm.psdb, diff.classaffirm.pmdb, diff.classaffirm.sp, diff.classaffirm.mg,
	diff.abortion.all, diff.abortion.evang, diff.abortion.female, diff.abortion.black, diff.abortion.loweduc, diff.abortion.pt, diff.abortion.psdb, diff.abortion.pmdb, diff.abortion.sp, diff.abortion.mg,
	diff.gaymarriage.all, diff.gaymarriage.evang, diff.gaymarriage.female, diff.gaymarriage.black, diff.gaymarriage.loweduc, diff.gaymarriage.pt, diff.gaymarriage.psdb, diff.gaymarriage.pmdb, diff.gaymarriage.sp, diff.gaymarriage.mg,
	diff.environment.all, diff.environment.evang, diff.environment.female, diff.environment.black, diff.environment.loweduc, diff.environment.pt, diff.environment.psdb, diff.environment.pmdb, diff.environment.sp, diff.environment.mg),ncol=10,byrow=T)
rownames(ecdf.table)<-c('Economic\nRegime\n(2002-13)','Ideology\n(2002-13)','Race\nQuotas\n(2010-13)','Class\nQuotas\n(2010-13)','Abortion\n(2010-13)','Gay\nMarriage\n(2010-13)','Environment\n(2011-13)')
colnames(ecdf.table)<-c('All','Evangelicals','Women','Afro-Brazilians','Low Education','PT','PSDB','PMDB','SP','MG')

# Plot
y<-matrix(7:1*2,ncol=10,nrow=7) + matrix(seq(-.9,.9,.2)*.75,ncol=10,nrow=7,byrow=T)

pdf(file='wtd_ecdf_diff_points_plot.pdf',height=10,width=7.5)
par(mar=c(5,6,2,2)+.1)
plot(ecdf.table,y,pch=matrix(c(8,21:24,21:23,21:22),nrow=7,ncol=10,byrow=T),bg=matrix(c(NA,rep('black',4),rep('grey',3),rep('white',2)),nrow=7,ncol=10,byrow=T),xlab='Distance Between Mass and Elite Respondents',yaxt='n',ylab='',xlim=c(0,.6),ylim=range(y))
abline(h=(9:2)*2-1,lty=3,lwd=1)
legend('topright',legend=c('All','Evangelical','Female','Afro-Brazilian','No College','PT','PSDB','PMDB','SP','MG'),pch=c(8,21:24,21:23,21:22),pt.bg=c(NA,rep('black',4),rep('grey',3),rep('white',2)),bg='white')
Axis(side=2,at=(7:1)*2,labels=rownames(ecdf.table),las=2,hadj=0,tick=F,line=4)
dev.off()

# ======================================================
# Appendix Figure 2: Opinion Congruence: Differences in
# Distributions Using Alternative Measures of Partisanship
# ======================================================

partyid.ecdf.table<-matrix(c(diff.gaymarriage.pt.lb, diff.gaymarriage.pt.ab, diff.gaymarriage.pt, diff.gaymarriage.psdb.lb, diff.gaymarriage.psdb.ab, diff.gaymarriage.psdb, diff.gaymarriage.pmdb.lb, diff.gaymarriage.pmdb.ab, diff.gaymarriage.pmdb, NA, NA, NA, diff.ideol.pt.lb, diff.ideol.pt.ab, diff.ideol.pt.lbab, diff.ideol.psdb.lb, diff.ideol.psdb.ab, diff.ideol.psdb.lbab, diff.ideol.pmdb.lb, diff.ideol.pmdb.ab, diff.ideol.pmdb.lbab),byrow=F,nrow=3)

pdf(file='partyid_ecdf_plot.pdf',height=6,width=7.5)
par(mar=c(5,6,2,2)+0.1)
partyid.ecdf.plot<- barplot(partyid.ecdf.table,beside=T,ylim=c(0,.6),names.arg=c('PT','PSDB','PMDB','','PT','PSDB','PMDB'),legend.text=c('Latinobarómetro (Intended Vote)','AmericasBarometer (Party Sympathy)','Both'),args.legend=list(x='topright'),ylab= expression(integral(abs(F[e](x)-F[m](x))*dx,0,1)))
axis(side=1, at= partyid.ecdf.plot[2,c(2,6)], tick=F, lwd=NULL, labels=c('Gay Marriage','Ideology'), line=2)
dev.off()

# ======================================================
# Appendix Figure 3: Opinion Congruence: Differences in 
# Distributions, Disaggregated by Legislature
# ======================================================

ecdf.table.disagg<-matrix(c(
	diff.econ.all, diff.econ.evang, diff.econ.female, diff.econ.black, diff.econ.loweduc, diff.econ.pt, diff.econ.psdb, diff.econ.pmdb, diff.econ.sp, diff.econ.mg,
	diff.econ.all.0205, diff.econ.evang.0205, diff.econ.female.0205, NA, diff.econ.loweduc.0205, diff.econ.pt.0205, diff.econ.psdb.0205, diff.econ.pmdb.0205, diff.econ.sp.0205, diff.econ.mg.0205,
	diff.econ.all.0609, diff.econ.evang.0609, diff.econ.female.0609, diff.econ.black.0609, diff.econ.loweduc.0609, diff.econ.pt.0609, diff.econ.psdb.0609, diff.econ.pmdb.0609, diff.econ.sp.0609, diff.econ.mg.0609,
	diff.econ.all.1013, diff.econ.evang.1013, diff.econ.female.1013, diff.econ.black.1013, diff.econ.loweduc.1013, diff.econ.pt.1013, diff.econ.psdb.1013, diff.econ.pmdb.1013, diff.econ.sp.1013, diff.econ.mg.1013,
	diff.ideol.all, diff.ideol.evang, diff.ideol.female, diff.ideol.black, diff.ideol.loweduc, diff.ideol.pt, diff.ideol.psdb, diff.ideol.pmdb, diff.ideol.sp, diff.ideol.mg,
	diff.ideol.all.0205, diff.ideol.evang.0205, diff.ideol.female.0205, NA, diff.ideol.loweduc.0205, diff.ideol.pt.0205, diff.ideol.psdb.0205, diff.ideol.pmdb.0205, diff.ideol.sp.0205, diff.ideol.mg.0205,
	diff.ideol.all.0609, diff.ideol.evang.0609, diff.ideol.female.0609, diff.ideol.black.0609, diff.ideol.loweduc.0609, diff.ideol.pt.0609, diff.ideol.psdb.0609, diff.ideol.pmdb.0609, diff.ideol.sp.0609, diff.ideol.mg.0609,
	diff.ideol.all.1013, diff.ideol.evang.1013, diff.ideol.female.1013, diff.ideol.black.1013, diff.ideol.loweduc.1013, diff.ideol.pt.1013, diff.ideol.psdb.1013, diff.ideol.pmdb.1013, diff.ideol.sp.1013, diff.ideol.mg.1013),ncol=10,byrow=T)

rownames(ecdf.table.disagg)<-c('Economic\nRegime\n(2002-13)','Economic\nRegime\n(2002-5)','Economic\nRegime\n(2006-9)','Economic\nRegime\n(2010-13)','Ideology\n(2002-13)','Ideology\n(2002-5)','Ideology\n(2006-9)','Ideology\n(2010-13)')
colnames(ecdf.table.disagg)<-c('All','Evangelicals','Women','Afro-Brazilians','Low Education','PT','PSDB','PMDB','SP','MG')

y<-matrix(8:1*2,ncol=10,nrow=8) + matrix(seq(-.9,.9,.2)*.75,ncol=10,nrow=8,byrow=T)

pdf(file='wtd_ecdf_diff_points_plot_disagg.pdf',height=10,width=7.5)
par(mar=c(5,6,2,2)+.1)
plot(ecdf.table.disagg,y,pch=matrix(c(8,21:24,21:23,21:22),nrow=8,ncol=10,byrow=T),bg=matrix(c(NA,rep('black',4),rep('grey',3),rep('white',2)),nrow=8,ncol=10,byrow=T),xlab='Distance Between Mass and Elite Respondents',yaxt='n',ylab='',xlim=c(0,.6),ylim=range(y))
abline(h=(9:2)*2-1,lty=3,lwd=1)
legend('topright',legend=c('All','Evangelical','Female','Afro-Brazilian','No College','PT','PSDB','PMDB','SP','MG'),pch=c(8,21:24,21:23,21:22),pt.bg=c(NA,rep('black',4),rep('grey',3),rep('white',2)),bg='white')
Axis(side=2,at=(8:1)*2,labels=rownames(ecdf.table.disagg),las=2,hadj=0,tick=F,line=4)
dev.off()

